/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.npi.admin.service.impl;

import com.neusoft.npi.admin.mapper.RoleMapper;
import com.neusoft.npi.admin.mapper.SysinfoMapper;
import com.neusoft.npi.admin.service.RoleService;
import com.neusoft.npi.common.entity.CommonResult;
import com.neusoft.npi.common.entity.ResultCode;
import com.neusoft.npi.common.entity.Role;
import com.neusoft.npi.common.entity.Sysinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired private RoleMapper roleMapper;
    @Autowired private SysinfoMapper sysinfoMapper;

    @Override
    public CommonResult findAllSysNodesList(Integer empno) {
        List<Sysinfo> list = roleMapper.findAllSysNodesList(empno);
        if (Objects.isNull(list) || list.size() == 0)
            return CommonResult.fail(ResultCode.NOT_FOUND);
        else return CommonResult.success(list);
    }

    @Override
    public CommonResult findAllRole() {
        List<Role> list = roleMapper.selectList(null);
        if (Objects.isNull(list) || list.size() == 0)
            return CommonResult.fail(ResultCode.NOT_FOUND);
        else return CommonResult.success(list);
    }

    @Override
    public CommonResult updateRole(Role role) {
        int i = roleMapper.updateById(role);
        if (i == 1) return CommonResult.success(role.getRoleid());
        return CommonResult.fail(ResultCode.UPDATE_BYKEY_FIALURE);
    }

    @Override
    public CommonResult findAllSysNodesTree() {
        List<Sysinfo> list = sysinfoMapper.selectList(null);
        //        从集合中找根节点
        Sysinfo root =
                list.stream()
                        .filter(sysinfo -> sysinfo.getParentid() == 0) //
                        .findFirst() // 短路计算
                        .orElse(null);
        list.remove(root);
        // 填充根节点的孩子
        fullChildren(root, list);

        return CommonResult.success(root);
    }

    @Override
    public CommonResult findAllSysnodeIdsByRoleId(Integer roleid) {
        List<Integer> list = roleMapper.findAllSysNodeIdsByRoleId(roleid);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult auth4Role(Integer roleid, List<Integer> sysids) {
        //        根据roleid清空关系表
        int i = roleMapper.deleteAuth4Role(roleid);
        //        批量插入
        int j = roleMapper.auth4Role(roleid, sysids);
        return null;
    }

    private void fullChildren(Sysinfo root, List<Sysinfo> list) {
        if (Objects.isNull(root)) return;
        list.forEach(
                sysinfo -> {
                    if (sysinfo.getParentid() == root.getSysid()) {
                        root.getChildren().add(sysinfo);
                    }
                });
        if (root.getChildren().size() == 0) return;
        root.getChildren()
                .forEach(
                        sysinfo -> {
                            fullChildren(sysinfo, list);
                        });
    }
}
